﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using CimeaSoft.BE;
using CimeaSoft.BL;
using Microsoft.Practices.Unity;
using System.Linq;

namespace CimeaSoft.UI
{
    public partial class FrmShowGeneral : Telerik.WinControls.UI.RadForm
    {
        [Dependency]
        public IMuestraResumenBL muesResumBL { get; set; }

        [Dependency]
        public IMuestraBL muestraBL { get; set; }

        public FrmShowGeneral()
        {
            InitializeComponent();
            this.BuilUp();
        }

        private void FrmShowGeneral_Load(object sender, EventArgs e)
        {
            muestraResumenBindingSource.DataSource = muesResumBL.GetAllFromMuestraResumen();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            muestraResumenBindingSource.DataSource = muesResumBL.GetAllFromMuestraResumenByCriterio(txtCriterio.Text);
        }

        private void txtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                muestraResumenBindingSource.DataSource = muesResumBL.GetAllFromMuestraResumenByCriterio(txtCriterio.Text);
            }
        }

        private void btnRptDetallado_Click(object sender, EventArgs e)
        {
            var currentMuestraResumen = muestraResumenBindingSource.Current as MuestraResumen;

            if (currentMuestraResumen != null)
            {
                Reportes.FrmReporteMuestra frm = new Reportes.FrmReporteMuestra();
                frm.ObtenerId(currentMuestraResumen.IdMuestra);
                frm.ShowDialog();
            }
            else MessageBox.Show("Debe seleccionar una Muestra", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void muestraResumenDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            try
            {
                for (int count = 0; count < muestraResumenDataGridView.Rows.Count; count++)
                {                  
                     var ihf = Convert.ToInt32(muestraResumenDataGridView.Rows[count].Cells[2].Value);
                    var qbr = Convert.ToInt32(muestraResumenDataGridView.Rows[count].Cells[3].Value);
                    var abi = Convert.ToInt32(muestraResumenDataGridView.Rows[count].Cells[4].Value);
                    var cera = Convert.ToString(muestraResumenDataGridView.Rows[count].Cells[5].Value);
                    #region colorIHF
                    if (ihf > 75)
                    {
                        muestraResumenDataGridView[2, count].Style.BackColor = Color.Blue;
                    }
                    else if (ihf >= 40 && ihf <= 75)
                    {
                        muestraResumenDataGridView[2, count].Style.BackColor = Color.Yellow;
                    }
                    else if (ihf < 40)
                    {
                        muestraResumenDataGridView[2, count].Style.BackColor = Color.Red;
                    }
                    #endregion

                    #region colorQBR-And
                    if (qbr > 75)
                    {
                        muestraResumenDataGridView[3, count].Style.BackColor = Color.Blue;
                    }
                    else if (qbr >= 45 && qbr <= 75)
                    {
                        muestraResumenDataGridView[3, count].Style.BackColor = Color.GreenYellow;
                    }
                    else if (qbr >= 27 && qbr <= 44)
                    {
                        muestraResumenDataGridView[3, count].Style.BackColor = Color.Yellow;
                    }
                    else if (qbr >= 11 && qbr <= 26)
                    {
                        muestraResumenDataGridView[3, count].Style.BackColor = Color.Orange;
                    }
                    else if (qbr < 11)
                    {
                        muestraResumenDataGridView[3, count].Style.BackColor = Color.Red;
                    }
                    #endregion

                    #region colorABI
                    if (abi > 74)
                    {
                        muestraResumenDataGridView[4, count].Style.BackColor = Color.Blue;
                    }
                    else if (abi >= 45 && abi <= 74)
                    {
                        muestraResumenDataGridView[4, count].Style.BackColor = Color.GreenYellow;
                    }
                    else if (abi >= 27 && abi <= 44)
                    {
                        muestraResumenDataGridView[4, count].Style.BackColor = Color.Yellow;
                    }
                    else if (abi >= 11 && abi <= 26)
                    {
                        muestraResumenDataGridView[4, count].Style.BackColor = Color.Orange;
                    }
                    else if (abi < 11)
                    {
                        muestraResumenDataGridView[4, count].Style.BackColor = Color.Red;
                    }
                    #endregion

                    #region colorCERA
                    if (cera.Contains("PESIMO"))
                    {
                        muestraResumenDataGridView[5, count].Style.BackColor = Color.Red;
                    }
                    else if (cera.Contains("MUY BUENO"))
                    {
                        muestraResumenDataGridView[5, count].Style.BackColor = Color.Blue;
                    }
                    else if (cera.Contains("MALO"))
                    {
                        muestraResumenDataGridView[5, count].Style.BackColor = Color.Orange;
                    }
                    else if (cera.Contains("REGULAR"))
                    {
                        muestraResumenDataGridView[5, count].Style.BackColor = Color.Yellow;
                    }
                    else if (cera.Contains("BUENO"))
                    {
                        muestraResumenDataGridView[5, count].Style.BackColor = Color.GreenYellow;
                    }
                    #endregion
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }
        }

        private void muestraResumenDataGridView_Click(object sender, EventArgs e)
        {
            try
            {
                for (int count = 0; count < muestraResumenDataGridView.Rows.Count; count++)
                {
                    var ihf = Convert.ToInt32(muestraResumenDataGridView.Rows[count].Cells[2].Value);
                    var qbr = Convert.ToInt32(muestraResumenDataGridView.Rows[count].Cells[3].Value);
                    var abi = Convert.ToInt32(muestraResumenDataGridView.Rows[count].Cells[4].Value);
                    var cera = Convert.ToString(muestraResumenDataGridView.Rows[count].Cells[5].Value);
                    #region colorIHF
                    if (ihf > 75)
                    {
                        muestraResumenDataGridView[2, count].Style.BackColor = Color.Blue;
                    }
                    else if (ihf >= 40 && ihf <= 75)
                    {
                        muestraResumenDataGridView[2, count].Style.BackColor = Color.Yellow;
                    }
                    else if (ihf < 40)
                    {
                        muestraResumenDataGridView[2, count].Style.BackColor = Color.Red;
                    }
                    #endregion

                    #region colorQBR-And
                    if (qbr > 75)
                    {
                        muestraResumenDataGridView[3, count].Style.BackColor = Color.Blue;
                    }
                    else if (qbr >= 45 && qbr <= 75)
                    {
                        muestraResumenDataGridView[3, count].Style.BackColor = Color.GreenYellow;
                    }
                    else if (qbr >= 27 && qbr <= 44)
                    {
                        muestraResumenDataGridView[3, count].Style.BackColor = Color.Yellow;
                    }
                    else if (qbr >= 11 && qbr <= 26)
                    {
                        muestraResumenDataGridView[3, count].Style.BackColor = Color.Orange;
                    }
                    else if (qbr < 11)
                    {
                        muestraResumenDataGridView[3, count].Style.BackColor = Color.Red;
                    }
                    #endregion

                    #region colorABI
                    if (abi > 74)
                    {
                        muestraResumenDataGridView[4, count].Style.BackColor = Color.Blue;
                    }
                    else if (abi >= 45 && abi <= 74)
                    {
                        muestraResumenDataGridView[4, count].Style.BackColor = Color.GreenYellow;
                    }
                    else if (abi >= 27 && abi <= 44)
                    {
                        muestraResumenDataGridView[4, count].Style.BackColor = Color.Yellow;
                    }
                    else if (abi >= 11 && abi <= 26)
                    {
                        muestraResumenDataGridView[4, count].Style.BackColor = Color.Orange;
                    }
                    else if (abi < 11)
                    {
                        muestraResumenDataGridView[4, count].Style.BackColor = Color.Red;
                    }
                    #endregion

                    #region colorCERA
                    if (cera.Contains("PESIMO"))
                    {
                        muestraResumenDataGridView[5, count].Style.BackColor = Color.Red;
                    }
                    else if (cera.Contains("MUY BUENO"))
                    {
                        muestraResumenDataGridView[5, count].Style.BackColor = Color.Blue;
                    }
                    else if (cera.Contains("MALO"))
                    {
                        muestraResumenDataGridView[5, count].Style.BackColor = Color.Orange;
                    }
                    else if (cera.Contains("REGULAR"))
                    {
                        muestraResumenDataGridView[5, count].Style.BackColor = Color.Yellow;
                    }
                    else if (cera.Contains("BUENO"))
                    {
                        muestraResumenDataGridView[5, count].Style.BackColor = Color.GreenYellow;
                    }
                    #endregion
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }
        }

        private void btnRptGeneral_Click(object sender, EventArgs e)
        {
            var muestras = (from c in muestraResumenBindingSource.DataSource as List<MuestraResumen>
                                where c.Chk==true
                                select c).ToList();

            var frm = new Reportes.FrmReporteCombina();
            frm.listaMuestras = muestras;
            frm.ShowDialog();
        }
    }
}
